<?php
namespace App\Controllers\Activity;

use App\Models\Activity\Activity;
use App\Libs\Views\Json;
/**
    //增加修改活动信息
 */
class CreateAlterActivity extends \App\Libs\Base\BaseAction
{

    public function execute()
    {

        $map = [
            'ligo'     => 'list_logo',
            'title'    => 'title',
            'content'  => 'content',
            'area'     => 'area_text',
            'price'    => 'ticket_price',
            'host'     =>'host',
            'bggo'     => 'background_logo',
            'trange'   => 'trange',
            'sttime'   => 'start_time',
            'odscore'  => 'order_score',
            'push'     => 'push',
        ];
        if($this->request->isPost()){
            $acid = $this->request->getPost('acid') ?? [];
            if(empty($acid)){
                $matchActivity = new Activity();
                $matchActivity->ctime = date('Y:m:d H:i:s');
                $matchActivity->utime = date('Y:m:d H:i:s');
            }else{
                $matchActivity = Activity::findOne($acid);
                $matchActivity->utime = date('Y:m:d H:i:s');
            }
            $post = $this->request->getPost();
            foreach ($map as $key => $val){
                if(isset($post[$val])){
                    $matchActivity->{$key} = $post[$val];
                }
            }
            if($matchActivity->save()){
                return Json::view(0,'操作成功',[]);
            }else{
                return Json::view(1,'操作失败',[]);
            }
        }else{
            if(!$this->request->has('acid')){
                return Json::view(1,'缺少acid：活动id',[]);
            }
            $acid = $this->request->get('acid');
            $activity = Activity::findOne($acid)->toArray();
            foreach ($map as $key => $val){
                if(isset($activity[$key])){
                    $activityNew[$val] = $activity[$key];
                }
            }
            $activityNew['id'] = $activity['id'];
            return Json::view(0,'ok',$activityNew);
        }



    }
}
